aboutsummaryrefslogtreecommitdiff
path: root/src/app/(main)/websites/[websiteId]/(reports)/goals/Goal.tsx
blob: b6c4a11d68aaf3cfff6a5d4e1d3a2fc4057b474e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Column, Dialog, Grid, Icon, ProgressBar, Row, Text } from '@umami/react-zen';
import { LoadingPanel } from '@/components/common/LoadingPanel';
import { useMessages, useResultQuery } from '@/components/hooks';
import { File, User } from '@/components/icons';
import { ReportEditButton } from '@/components/input/ReportEditButton';
import { Lightning } from '@/components/svg';
import { formatLongNumber } from '@/lib/format';
import { GoalEditForm } from './GoalEditForm';

export interface GoalProps {
  id: string;
  name: string;
  type: string;
  parameters: {
    name: string;
    type: string;
    value: string;
  };
  websiteId: string;
  startDate: Date;
  endDate: Date;
}

export type GoalData = { num: number; total: number };

export function Goal({ id, name, type, parameters, websiteId, startDate, endDate }: GoalProps) {
  const { formatMessage, labels } = useMessages();
  const { data, error, isLoading, isFetching } = useResultQuery<GoalData>(type, {
    websiteId,
    startDate,
    endDate,
    ...parameters,
  });
  const isPage = parameters?.type === 'path';

  return (
    <LoadingPanel data={data} isLoading={isLoading} isFetching={isFetching} error={error}>
      {data && (
        <Grid gap>
          <Grid columns="1fr auto" gap>
            <Column gap>
              <Row>
                <Text size="4" weight="bold">
                  {name}
                </Text>
              </Row>
            </Column>
            <Column>
              <ReportEditButton id={id} name={name} type={type}>
                {({ close }) => {
                  return (
                    <Dialog
                      title={formatMessage(labels.goal)}
                      variant="modal"
                      style={{ minHeight: 300, minWidth: 400 }}
                    >
                      <GoalEditForm id={id} websiteId={websiteId} onClose={close} />
                    </Dialog>
                  );
                }}
              </ReportEditButton>
            </Column>
          </Grid>
          <Row alignItems="center" justifyContent="space-between" gap>
            <Text color="muted">
              {formatMessage(isPage ? labels.viewedPage : labels.triggeredEvent)}
            </Text>
            <Text color="muted">{formatMessage(labels.conversionRate)}</Text>
          </Row>
          <Row alignItems="center" justifyContent="space-between" gap>
            <Row alignItems="center" gap>
              <Icon>{parameters.type === 'path' ? <File /> : <Lightning />}</Icon>
              <Text>{parameters.value}</Text>
            </Row>
            <Row alignItems="center" gap>
              <Icon>
                <User />
              </Icon>
              <Text title={`${data?.num} / ${data?.total}`}>{`${formatLongNumber(
                data?.num,
              )} / ${formatLongNumber(data?.total)}`}</Text>
            </Row>
          </Row>
          <Row alignItems="center" gap="6">
            <ProgressBar
              value={data?.num || 0}
              minValue={0}
              maxValue={data?.total || 1}
              style={{ width: '100%' }}
            />
            <Text weight="bold" size="7">
              {data?.total ? Math.round((+data?.num / +data?.total) * 100) : '0'}%
            </Text>
          </Row>
        </Grid>
      )}
    </LoadingPanel>
  );
}